package be.groept.ace.enquete.model.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import be.groept.ace.enquete.model.enums.Language;
import be.groept.ace.enquete.model.interfaces.PersistentieValideerbaar;
import be.groept.ace.enquete.utils.ServletUtils;
import be.groept.ace.enquete.utils.interfaces.FormValideerbaar;

public class User implements Comparable<User>, Serializable, FormValideerbaar, PersistentieValideerbaar {
	private static final long serialVersionUID = 1L;
	private int id = -1;
	private String name;
	private String firstname;
	private String address;
	private Language language;
	private String email;
	private String password;
	private boolean administrator;
	private Date creation;
	
	private SortedSet<UserSurvey> userSurveySet;
	private SortedSet<Answer> answerSet;
	
	public User() {
		userSurveySet = new TreeSet<UserSurvey>();
		answerSet = new TreeSet<Answer>();
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Language getLanguage() {
		return language;
	}
	public void setLanguage(Language language) {
		this.language = language;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email.toLowerCase();
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public boolean isAdministrator() {
		return administrator;
	}
	public void setAdministrator(boolean administrator) {
		this.administrator = administrator;
	}
	public Date getCreation() {
		return creation;
	}
	public void setCreation(Date creation) {
		this.creation = creation;
	}
	
	public void addUserSurvey(UserSurvey userSurvey) {
		userSurveySet.add(userSurvey);
	}
	
	public void removeUserSurvey(UserSurvey userSurvey) {
		userSurveySet.remove(userSurvey);
	}
	
	public SortedSet<UserSurvey> getUserSurveySet() {
		return userSurveySet;
	}
	
	public boolean containsUserSurvey(UserSurvey userSurvey) {
		return userSurveySet.contains(userSurvey);
	}
	
	public void addAnswer(Answer answer) {
		answerSet.add(answer);
	}
	
	public void removeAnswer(Answer answer) {
		answerSet.remove(answer);
	}
	
	public SortedSet<Answer> getAnswerSet() {
		return answerSet;
	}
	
	public boolean containsAnswer(Answer answer) {
		return answerSet.contains(answer);
	}
	
	@Override
	public int hashCode() {
		return getEmail().hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if( getClass() != obj.getClass()) {
			return false;
		}
		User other = (User)obj;
		return getEmail().equals(other.getEmail());
	}

	//default sort by name asc
	@Override
	public int compareTo(User o) {
		return getName().compareTo(o.getName());
	}
	
	@Override
	public String toString() {
		return String.format("User object - id: %1$d - name: %2$s - firstname: %3$s", getId(), getName(), getFirstname());
	}

	@Override
	public boolean isValid(List<String> errors) {
		return isValidForForm(errors);
	}

	@Override
	public boolean isValidForForm(List<String> errors) {
		boolean valid = true;
		if(ServletUtils.isNullOrEmpty(getName())) {
			errors.add("Achternaam mag niet leeg zijn");
			valid = false;
		}
		if(ServletUtils.isNullOrEmpty(getFirstname())) {
			errors.add("Voornaam mag niet leeg zijn");
			valid = false;
		}
		if(ServletUtils.isNullOrEmpty(getEmail())) {
			errors.add("Email mag niet leeg zijn");
			valid = false;
		}
		if(ServletUtils.isNullOrEmpty(getPassword())) {
			errors.add("Paswoord mag niet leeg zijn");
			valid = false;
		}
		return valid;
	}
}
